package com.base.myproject.client.HR.bcode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.LasyObejct;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Label;

public class Bcode_SalaryRecord extends LayoutContainer implements LasyObejct {
	
	BaseStore<BusiObject> mainstore = null;
	Button search = new Button("查询");
	DateField begin = new DateField();
	DateField end = new DateField();
	
	public Bcode_SalaryRecord(BaseStore<BusiObject> mainstore){
		this.mainstore = mainstore;
		search.addListener(Events.OnClick, getListener());
		init();
	}

	public void init() {
		initWidget();
	}
	
	public void initWidget(){
		VerticalPanel v = new VerticalPanel();
		HorizontalPanel hp = new HorizontalPanel();
		v.setHorizontalAlign(HorizontalAlignment.LEFT);
		v.add(createForm());
		hp.add(createSalaryGrid());
		hp.add(createGrid());
		v.add(hp);
		v.setSpacing(2);
		this.add(v);
	}
	
	BaseFormPanel form = null;

	BaseFormPanel createForm() {
		form = new BaseFormPanel(mainstore);
		form.setWidth(760);
		form.setPadding(6);
		form.setStyleName("form_css2");
		form.setBodyBorder(false);
		TableLayout tl = new TableLayout(5);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("从:"), td);
		
		begin.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		form.add(begin,td);
		form.add(new Label("到:"), td);
		
		end.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		form.add(end,td);
		search.setWidth(60);
		form.add(search, td);

	//	form.inited();
		return form;
	};
	
	BaseStore<BusiObject> Salarystore = null;

	BaseStore<BusiObject> getSalaryStore() {
		if (Salarystore == null) {
			createSalaryStore();
		}
		return Salarystore;
	}

	BaseStore<BusiObject> createSalaryStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("upcode");
		type.getField("upcode").setType(String.class);
		type.addField("amount1");
		type.getField("amount1").setType(Double.class);
		type.addField("amount2");
		type.getField("amount2").setType(Double.class);
		type.addField("amount3");
		type.getField("amount3").setType(Double.class);
		type.addField("amount4");
		type.getField("amount4").setType(Double.class);
		type.addField("amount5");
		type.getField("amount5").setType(Double.class);
		type.addField("total");
		type.getField("total").setType(Double.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("null");
		info.setKeyname(new String[] { "icode" });
		info
				.setSql("select bcode,upcode,sum(isnull(amount1,0)) as amount1,sum(isnull(amount2,0)) as amount2,sum(isnull(amount3,0)) as amount3,sum(isnull(amount4,0)) as amount4,sum(isnull(amount5,0)) as amount5  from (select bcode, upcode,case when salarytype='1' then sum(amount) end as amount1,  case when salarytype='2' then sum(amount) end as amount2, case when salarytype='3' then sum(amount) end as amount3, case when salarytype='4' then sum(amount) end as amount4,case when salarytype='5' then sum(amount) end as amount5 from hr_salary where  bcode='{bcode}' and 1=0 group by bcode,upcode,salarytype )as a group by bcode,upcode");
		info.setForcenolimit("true");
		info.setParent(mainstore);// 关联主表
		info.setLinkparentcolumns(new String[] { "bcode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "bcode" });//
		// 关联主表字段
		Salarystore = new BaseStore<BusiObject>(type, info);
	
		return Salarystore;
	}

	BaseGrid<BusiObject> Salarygrid = null;

	BaseGrid<BusiObject> createSalaryGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C1 = new ColumnConfig("upcode", "发薪编号", 80);
		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("amount1", "工资", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("amount2", "津贴", 80);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("amount3", "社保", 80);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("amount4", "个税", 80);
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("amount5", "花红", 80);
		columns.add(C6);
		ColumnConfig C7 = new ColumnConfig("total", "合计", 80);
		columns.add(C7);
		
		ColumnModel cm = new ColumnModel(columns);
		Salarygrid = new BaseGrid<BusiObject>(getSalaryStore(), cm,false,false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		Salarygrid.setSelectionModel(cbs);
		Salarygrid.setHeight(315);
		Salarygrid.setWidth(610);
		return Salarygrid;
	}
	BaseStore<BusiObject> hr_salarystore = null;

	BaseStore<BusiObject> getStore() {
		if (hr_salarystore == null) {
			createhr_salaryStore();
		}
		return hr_salarystore;
	}

	BaseStore<BusiObject> createhr_salaryStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("allowanceno_");
		type.getField("allowanceno_").setType(String.class);
		type.addField("amount");
		type.getField("amount").setType(Double.class);
		type.addField("status");
		type.getField("status").setType(Integer.class);
		type.addField("vprepare");
		type.getField("vprepare").setType(String.class);
		type.addField("salarydate");
		type.getField("salarydate").setType(Date.class);
		type.addField("upcode");
		type.getField("upcode").setType(String.class);
		type.addField("bname");
		type.getField("bname").setType(String.class);
		type.addField("desc_cn");
		type.getField("desc_cn").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_salary");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_salary where amount != 0 and bcode='{bcode}' and   upcode='{upcode}'");
		info.setParent(getSalaryStore());// 关联主表
		info.setLinkparentcolumns(new String[] { "bcode","upcode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "bcode","upcode" });// 关联主表字段
		hr_salarystore = new BaseStore<BusiObject>(type, info);
		return hr_salarystore;
	}
	
	BaseGrid<BusiObject> salarydetailgrid = null;

	BaseGrid<BusiObject> createGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C3 = new ColumnConfig("allowanceno_", "薪金类别", 80);
		BaseComboBoxForm<BaseModelData> bsb3 = new BaseComboBoxForm<BaseModelData>("code","desc_cn","hr_salary_item","",true);
		
		C3.setRenderer(bsb3);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("amount", "金额", 60);
		NumberField C_num4 = new  NumberField(); 
		C4.setEditor(new CellEditor(C_num4));
		columns.add(C4);

		ColumnModel cm = new ColumnModel(columns)
		{
			 public String getColumnStyle(int colIndex) {
				 if(colIndex==4)
				 {
					
					    return " background-color: #FFFF99 ;";
				 }
				
				    return configs.get(colIndex).getStyle();

				  }
		};
		salarydetailgrid = new BaseGrid<BusiObject>(createhr_salaryStore(), cm, false, false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		salarydetailgrid.setSelectionModel(cbs);
		salarydetailgrid.setHeight(315);
		salarydetailgrid.setWidth(200);
		
		return salarydetailgrid;
	}
	
	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.OnClick) {
							if (be.getSource().equals(search)) {
								Object bcode = mainstore.getValue("bcode");
								if(!begin.getRawValue().equals("")  && !end.getRawValue().equals("")){
									
									DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");
									Date beginTime = begin.getValue();
									Date endTime = end.getValue();
									if(beginTime.compareTo(endTime) == 1){
										MessageBox.alert("Error", "开始时间不能大于结束时间！", null);
									}else{
										String sql =("select bcode,upcode,amount1,amount2,amount3,amount4,amount5,(amount1%2Bamount2%2Bamount3%2Bamount4%2Bamount5)as total " +
												"from(select bcode,upcode,sum(isnull(amount1,0)) as amount1,sum(isnull(amount2,0)) as amount2," +
												"sum(isnull(amount3,0)) as amount3,sum(isnull(amount4,0)) as amount4,sum(isnull(amount5,0)) as amount5" +
												" from (select bcode, upcode,case when salarytype='1' then sum(amount) end as amount1, " +
												" case when salarytype='2' then sum(amount) end as amount2, case when salarytype='3' then sum(amount) end as amount3, " +
												"case when salarytype='4' then sum(amount) end as amount4,case when salarytype='5' then sum(amount) end as amount5 " +
												" from hr_salary where bcode='"+bcode+"' and exists(select 1 from hr_salary_main_c where code = upcode and start_time >= '"+dtf.format(beginTime)+"' and end_time <= '"+dtf.format(endTime)+"') group by bcode,upcode,salarytype )as a group by bcode,upcode)as b");
												
										Salarystore.getStoreinfo().setSql(sql);
										Salarystore.reload();
									}
									
								}
							}
						} 
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
	
	@Override
	public boolean isopen() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean open() {
		// TODO Auto-generated method stub
		return false;
	}

}
